#MP Moralization and Foreign Policy Attitudes Replication File: Cleaning and Coding the Data for Study 1
####Last updated: 10 June 2025

###Analyses carried out using R version 4.4.3 in RStudio version Version 2024.04.2+764 on Lenovo ThinkPad X1 Carbon Gen 12 Intel Core Ultra 7 155U, 1700 Mhz, 12 running Windows 11 Enterprize

#Uncomment below to install packages as needed
#install.packages("ggplot2")
#install.packages("stargazer")
#install.packages("gridExtra")
#install.packages("xtable")
#install.packages("psych")
#install.packages("reshape2")
#install.packages("lmtest")
#install.packages("sandwich")
#install.packages("multcomp")
#install.packages("ggstance")
#install.packages("plyr")
#install.packages("car")


library(foreign)
library(ggplot2)
library(stargazer)
library(gridExtra)
library(xtable)
library(psych)
library(reshape2)
library(lmtest)
library(sandwich)
library(multcomp)
library(ggstance)
library(plyr)
library(car)



#Load the data
setwd() #Set your working directory to the folder containing the data
data <- read.csv("MP_study1_data.csv")
regions <- read.csv("MP_study1_regions_coded.csv")

colnames(data) 
colnames(regions)

# Prune the data  ---------------------------------------------------------
nrow(data) #4010

#Retain observations from those who consented to participate
x <- which(data$consent==1)
data <- data[x,] #3664 

#Drop participants flagged as bots by recaptcha score < 0.5
x <- which(data$Q_RecaptchaScore<0.5)
data <- data[-x,] #3635 

#Drop participants flagged as bots by relevantID fraud score (>= 30)
x <- which(data$Q_RelevantIDFraudScore<30)
data <- data[x,] #3563 

#Drop duplicates flagged by Qualtrics (>=75)
x <- which(data$Q_RelevantIDDuplicateScore<75)
data <- data[x,] #3555 

#Respondents failing either screener immediately terminated; check rate of termination:

#code for screener1; correct if 1 in both screener1_1 AND screener1_2 (selected "extremely interested" and "very interested", did not select any other options); code "NA" if did not select any options, and 0 if selected anything besides 1 AND 2
data$screener1correct <- ifelse((data$screener1_1==1 & data$screener1_2==1) & (is.na(data$screener1_3) & is.na(data$screener1_4) & is.na(data$screener1_5)), 1,
                                ifelse(is.na(data$screener1_1) & is.na(data$screener1_2) & is.na(data$screener1_3) & is.na(data$screener1_4) & is.na(data$screener1_5), NA, 0))

round(prop.table(table(data$screener1correct, useNA="ifany")), digits=3)*100 #74.4% passed, 9.1% failed, 19.9% NAs

#Correct response is coded 7 - "not at all accurate"
round(prop.table(table(data$screener2, useNA="ifany")), digits=3)*100 # 60% passed screener2

#Pre-registration: "Respondents who fail either of the first two attention checks (pre-treatment) will be terminated from the survey and excluded from analysis."
x <- which(data$screener1correct==1)
data <- data[x,] #2524 

x <- which(data$screener2==7)
data <- data[x,] #2132 



# Variable Coding Functions ----------------------------------------------------

#Function to rescale variables to range from 0 to 1
rescale <- function(x){
  return((x-min(x,na.rm=TRUE))/(max(x-min(x,na.rm=TRUE),na.rm=TRUE)))
}

#Function to recode variables
recode <- function(variable, reverse=FALSE, maxVal, minVal, binarize=FALSE, x=NA, x2){
  if (is.factor(variable)){variable <- as.numeric(variable)}
  if (missing(maxVal)){maxVal <- max(variable, na.rm=TRUE)}
  if (missing(minVal)){minVal <- min(variable, na.rm=TRUE)}
  variable[variable > maxVal] <- NA
  variable[variable < minVal] <- NA
  if (reverse){
    temp <- variable
    for (j in 1:maxVal){
      temp[which(variable==j)] <- maxVal-j+1
    }
    return(temp)
  }
  if (binarize){
    if (is.na(x)){stop("Specify the value to dichotomize on")}
    temp <- variable
    if (missing(x2)){
      i <- which(variable == x)
      k <- which(variable < x | variable > x)
    }
    else{
      i <- which(variable >= x & variable <= x2)
      k <- which(variable < x | variable > x2)
    }
    temp[i] <- 1
    temp[k] <- 0
    return(temp)
  }
  return(variable)
}



# Data summary function ---------------------------------------------------

#Function to plot the means with 95% confidence intervals
summarySE <- function(data=NULL, measurevar, groupvars=NULL, na.rm=FALSE,
                      conf.interval=.95, .drop=TRUE) {
  
  # New version of length which can handle NA's: if na.rm==T, don't count them
  length2 <- function (x, na.rm=FALSE) {
    if (na.rm) sum(!is.na(x))
    else       length(x)
  }
  
  # This does the summary. For each group's data frame, return a vector with
  # N, mean, and sd
  datac <- ddply(data, groupvars, .drop=.drop,
                 .fun = function(xx, col) {
                   c(N    = length2(xx[[col]], na.rm=na.rm),
                     mean = mean   (xx[[col]], na.rm=na.rm),
                     sd   = sd     (xx[[col]], na.rm=na.rm)
                   )
                 },
                 measurevar
  )
  
  # Rename the "mean" column    
  datac <- rename(datac, c("mean" = measurevar))
  
  datac$se <- datac$sd / sqrt(datac$N)  # Calculate standard error of the mean
  
  # Confidence interval multiplier for standard error
  # Calculate t-statistic for confidence interval: 
  # e.g., if conf.interval is .95, use .975 (above/below), and use df=N-1
  ciMult <- qt(conf.interval/2 + .5, datac$N-1)
  datac$ci <- datac$se * ciMult
  
  return(datac)
}



# Function to return number of significant p values -----------------------
null_count <- function(model_summary, exclude_intercept=FALSE) {
  pvals <- model_summary$coefficients[, "Pr(>|t|)"]  
  if(exclude_intercept) {
    pvals <- pvals[-1]
  }
  sum(pvals > 0.05)
}

sig_count <- function(model_summary, exclude_intercept=FALSE) {
  pvals <- model_summary$coefficients[, "Pr(>|t|)"]   
  if(exclude_intercept) {
    pvals <- pvals[-1]
  }
  sum(pvals <= 0.05)
}

# Variable Coding: Demographics ------------------------------------------------------------

#Gender
data$man <- recode(data$gender, binarize =TRUE, x="1") #1 if man, 0 if else
data$woman <- recode(data$gender, binarize =TRUE, x="2") #1 if woman, 0 if else


#Age
data$age1 <- recode(data$age, binarize=TRUE, x=1) #18-24
data$age2 <- recode(data$age, binarize=TRUE, x=2) #25-34
data$age3 <- recode(data$age, binarize=TRUE, x=3) #35-44
data$age4 <- recode(data$age, binarize=TRUE, x=4) #45-54
data$age5 <- recode(data$age, binarize=TRUE, x=5) #55-64
data$age6 <- recode(data$age, binarize=TRUE, x=6, x2=8) #65 + 


#Race
data$white <- recode(data$race, binarize=TRUE, x=3) #White/Caucasian
data$black <- recode(data$race, binarize=TRUE, x=1) #Black or African American
data$asian <- recode(data$race, binarize=TRUE, x=2) #Asian
data$other <- recode(data$race, binarize=TRUE, x=4, x2=7) #American Indian or Alaska Native, Pacific Islander, Multi-Racial, Other

data$racenwb <- ifelse(data$asian==1 | data$other==1, 1,
                       ifelse(data$asian==0 & data$other==0, 0, NA))

#Latino origin
data$latinor <- recode(data$latino, binarize=TRUE, x=2) # Yes hispanic or latino origin

#Non-hispanic white
data$nhwhite <- ifelse(data$white==1 & data$latinor==0, 1, 
                       ifelse(data$white==0 | (data$white==1 & data$latinor==1), 0, NA))


#Foreign policy orientations
data$mi1 <- rescale(with(data,apply(cbind(mil1, mil2, mil3), 1, sum))) #Militant Internationalism Scale
data$ci1 <- rescale(with(data,apply(cbind(cint1, cint2, cint3), 1, sum))) #Cooperative Internationalism Scale
data$isolat1 <- rescale(with(data, apply(cbind(iso1, iso2, iso3), 1, sum))) #Isolationism Scale

#Party 
data$rep <- recode(data$pid3, binarize=TRUE, x=1) #Republican
data$dem <- recode(data$pid3, binarize=TRUE, x=2) #Democrat
data$ind <- recode(data$pid3, binarize=TRUE, x=3, x2=4) #Independent or something else

data$pid7 <- ifelse(data$rep==1 & data$pidrep==1, 7, #strong rep
                    ifelse(data$rep==1 & data$pidrep==2, 6, #not strong rep
                           ifelse(data$ind==1 & data$pidlean==2, 5, #ind leaning rep
                                  ifelse(data$ind==1 & data$pidlean==3, 4, #ind
                                         ifelse(data$ind==1 & data$pidlean==1, 3, #ind leaning dem
                                                ifelse(data$dem==1 & data$piddem==2, 2, #not strong dem
                                                       ifelse(data$dem==1 & data$piddem==1, 1, NA))))))) #strong dem
data$pid71 <- rescale(data$pid7) #7 point party scale, higher values = strong republican

#Party Dummies with Leaners
data$pidreplean <- ifelse(data$pid7 > 4, 1,
                          ifelse(data$pid7 < 5, 0, NA))
data$piddemlean <- ifelse(data$pid7 < 4, 1,
                          ifelse(data$pid7  >3 , 0, NA))
data$pidtrueind <- ifelse(data$pid7 == 4, 1,
                          ifelse(data$pid7>4 | data$pid7<4 , 0, NA))


#Religiosity
data$bornagain <- recode(data$religion_1, binarize = TRUE, x=1) #born-again Christian
data$rel_impr <- rescale(recode(data$religion_2, reverse=TRUE)) #Higher: Religion is very important to you
data$religion_3r <- ifelse(data$religion_3==7, 4, data$religion_3) #Recode "don't know" to scale midpoint
data$rel_attendr <- rescale(recode(data$religion_3r, reverse=TRUE)) #Higher: Attend services more than once a week

#Ideology
data$ideo1 <- rescale(data$ideo) #Higher values = more liberal

#Political interest
data$polint1 <- rescale(data$polint) #Higher values = extremely interested

#Demographic Block 2 (measured post-foreign policy survey)

#Education
data$university <- recode(data$educ, binarize=TRUE, x=5, x2=7) #university education

#Generation
data$gensilent <- recode(data$age_year, binarize = TRUE, x=min(data$age_year, na.rm=TRUE), x2=1945) #silent generation
data$genboom<- recode(data$age_year, binarize = TRUE, x=1946, x2=1964) #baby boomer generation
data$genx <- recode(data$age_year, binarize = TRUE, x=1965, x2=1980) #generation X
data$genmill <-recode(data$age_year, binarize = TRUE, x=1981, x2=1996) #millennial generation
data$genz <-recode(data$age_year, binarize = TRUE, x=1997, x2=max(data$age_year, na.rm=TRUE)) #generation Z


#Income
table(data$income, useNA="ifany")
data$inc1 <- recode(data$income, binarize=TRUE, x=1, x2=4) #Income < $30,000
data$inc2 <- recode(data$income, binarize=TRUE, x=5, x2=8) #Income $30,000 - $60,000
data$inc3 <- recode(data$income, binarize=TRUE, x=9, x2=11) #Income $60,000 - 100,000
data$inc4 <- recode(data$income, binarize=TRUE, x=12, x2=15) #Income > $100,000
data$inc_no <- recode(data$income, binarize=TRUE, x=16) #Income: prefer not to say

data$inc1 <- ifelse(data$inc_no==1, NA, data$inc1)
data$inc2 <- ifelse(data$inc_no==1, NA, data$inc2)
data$inc3 <- ifelse(data$inc_no==1, NA, data$inc3)
data$inc4 <- ifelse(data$inc_no==1, NA, data$inc4)

#Region - hand-coded by research assistant based on open-ended data$state and US census regions. 
#NA responses for "United States" or other non-categorizable responses

#Convert empty cells for state to NA: 
data$state[data$state==""] <- NA

data <- merge(data, regions, by=c("ResponseId", "state"))

table(data$region, useNA="ifany")
data$regionf <- factor(ifelse(data$region==1, "west",
                              ifelse(data$region==2, "midwest",
                                     ifelse(data$region==3, "south",
                                            ifelse(data$region==4, "northeast", NA)))))



# Variable Coding: Foreign Policy and Moralization Questionnaire ---------------------------
###Human Rights
#Support
data$hr1r <- rescale(ifelse(data$hr1==6, 3, data$hr1)) #Support defending human rights; Recode Dk to midpoint
data$hr1mid <- recode(data$hr1, binarize=TRUE, x=3) #dummy for midpoint

data$hr2r <- rescale(ifelse(data$hr2==6, 3, data$hr1)) #Support decent work protections; Recode Dk to midpoint
data$hr2mid <- recode(data$hr2, binarize=TRUE, x=3) #dummy for mid

#Moral conviction:
data$hr1moral <- rescale(with(data,apply(cbind(hr1_core, hr1_fund), 1, sum)))
data$hr2moral <- rescale(with(data,apply(cbind(hr2_core, hr2_fund), 1, sum)))

###Climate
#Support
data$climate1r <- rescale(ifelse(data$climate1==6, 3, data$climate1)) #Support reduce fossil fuels; Recode Dk to midpoint
data$climate1mid <- recode(data$climate1, binarize=TRUE, x=3) #dummy for mid

data$climate2r <- rescale(ifelse(data$climate2==6, 3, data$climate1)) #Support biodiversity; Recode Dk to midpoint
data$climate2mid <- recode(data$climate2, binarize=TRUE, x=3) #dummy for mid


#Moral conviction:
data$climate1moral <- rescale(with(data,apply(cbind(climate1_core, climate1_fund), 1, sum)))
data$climate2moral <- rescale(with(data,apply(cbind(climate2_core, climate2_fund), 1, sum)))


###Aid
#Support
data$aid1r <- rescale(ifelse(data$aid1==6, 3, data$aid1)) #Support digital inclusion programs; Recode Dk to midpoint
data$aid1mid <- recode(data$aid1, binarize=TRUE, x=3) #dummy for midpoint

data$aid2r <- rescale(ifelse(data$aid2==6, 3, data$aid1)) #Support vaccine programs; Recode Dk to midpoint
data$aid2mid <- recode(data$aid2, binarize=TRUE, x=3) #dummy for midpoint

#Moral conviction:
data$aid1moral <- rescale(with(data,apply(cbind(aid1_core, aid1_fund), 1, sum)))
data$aid2moral <- rescale(with(data,apply(cbind(aid2_core, aid2_fund), 1, sum)))

###Security
#Support
data$security1r <- rescale(ifelse(data$security1==6, 3, data$security1)) #Support counter-terrorism; Recode Dk to midpoint
data$security1mid <- recode(data$security1, binarize=TRUE, x=3) #dummy for midpoint

data$security2r <- rescale(ifelse(data$security2==6, 3, data$security1)) #Support nuclear sabotage; Recode Dk to midpoint
data$security2mid <- recode(data$security2, binarize=TRUE, x=3) #dummy for midpoint

#Moral conviction:
data$security1moral <- rescale(with(data,apply(cbind(security1_core, security1_fund), 1, sum)))
data$security2moral <- rescale(with(data,apply(cbind(security2_core, security2_fund), 1, sum)))


###Cooperation
#Support
data$cooperation1r <- rescale(ifelse(data$cooperation1==6, 3, data$cooperation1)) #Support institutions; Recode Dk to midpoint
data$cooperation1mid <- recode(data$cooperation1, binarize=TRUE, x=3) #dummy for midpoint

data$cooperation2r <- rescale(ifelse(data$cooperation2==6, 3, data$cooperation1)) #Support ICJ accountability; Recode Dk to midpoint
data$cooperation2mid <- recode(data$cooperation2, binarize=TRUE, x=3) #dummy for midpoint


#Moral conviction:
data$cooperation1moral <- rescale(with(data,apply(cbind(cooperation1_core, cooperation1_fund), 1, sum)))
data$cooperation2moral <- rescale(with(data,apply(cbind(cooperation2_core, cooperation2_fund), 1, sum)))

###Economic
#Support
data$economic1r <- rescale(ifelse(data$economic1==6, 3, data$economic1)) #Support lifting tariffs; Recode Dk to midpoint
data$economic1mid <- recode(data$economic1, binarize=TRUE, x=3) #dummy for midpoint

data$economic2r <- rescale(ifelse(data$economic2==6, 3, data$economic1)) #Support FDI limits; Recode Dk to midpoint
data$economic2mid <- recode(data$economic2, binarize=TRUE, x=3) #dummy for mid

#Moral conviction:
data$economic1moral <- rescale(with(data,apply(cbind(economic1_core, economic1_fund), 1, sum)))
data$economic2moral <- rescale(with(data,apply(cbind(economic2_core, economic2_fund), 1, sum)))



# Variable Coding: Domestic issues ---------------------------------------------------------
###Social Security
#Support
data$socialsec1r <- rescale(ifelse(data$socialsec==6, 3, data$socialsec)) #Support raising taxes for social security; Recode Dk to midpoint

#Moral conviction:
data$socialsecmoral <- rescale(with(data,apply(cbind(socialsec_core, socialsec_fund), 1, sum)))

###Stem Cell Research
#Support
data$stemcell2r <- rescale(ifelse(data$stemcell==6, 3, data$stemcell)) #Support federak funding for stem cell research; Recode Dk to midpoint

#Moral conviction:
data$stemcellmoral <- rescale(with(data,apply(cbind(stemcell_core, stemcell_fund), 1, sum)))


# Variable Coding: Salience ----------------------------------------------------------------

data$salhr1 <- rescale(data$salience_1) #protect human rights
data$salhr2 <- rescale(data$salience_2) #decent work initiatives
data$salclimate1 <- rescale(data$salience_3) #fossil fuels
data$salclimate2 <- rescale(data$salience_4) #biodiversity
data$salaid1 <- rescale(data$salience_5) #digital inclusion
data$salaid2 <- rescale(data$salience_6) #global vaccines
data$salsecurity1 <- rescale(data$salience_7) #counter-terrorism
data$salsecurity2 <- rescale(data$salience_8) #nuclear sabotage
data$salcooperation1 <- rescale(data$salience_9) #the UN
data$salcooperation2 <- rescale(data$salience_10) #the ICJ
data$saleconomic1 <- rescale(data$salience_11) #tariffs
data$saleconomic2 <- rescale(data$salience_12) #FDI



# Variable Coding:Treatments --------------------------------------------------------------

#Human rights: 
data$treat_hr1 <- ifelse(data$country1=="another country", "control",
                         ifelse(data$country1=="Canada" | data$country1=="Great Britain" | data$country1=="Japan", "friend",
                                ifelse(data$country1=="China" | data$country1=="Iran" | data$country1=="Russia", "adversary", NA)))
data$treat_hr2 <- ifelse(data$country2=="another country", "control",
                         ifelse(data$country2=="Canada" | data$country2=="Great Britain" | data$country2=="Japan", "friend",
                                ifelse(data$country2=="China" | data$country2=="Iran" | data$country2=="Russia", "adversary", NA)))

#Climate
data$treat_climate1 <- ifelse(data$country3=="another country", "control",
                              ifelse(data$country3=="Canada" | data$country3=="Great Britain" | data$country3=="Japan", "friend",
                                     ifelse(data$country3=="China" | data$country3=="Iran" | data$country3=="Russia", "adversary", NA)))
data$treat_climate2 <- ifelse(data$country4=="another country", "control",
                              ifelse(data$country4=="Canada" | data$country4=="Great Britain" | data$country4=="Japan", "friend",
                                     ifelse(data$country4=="China" | data$country4=="Iran" | data$country4=="Russia", "adversary", NA)))

#Aid
data$treat_aid1 <- ifelse(data$country5=="another country", "control",
                          ifelse(data$country5=="Canada" | data$country5=="Great Britain" | data$country5=="Japan", "friend",
                                 ifelse(data$country5=="China" | data$country5=="Iran" | data$country5=="Russia", "adversary", NA)))
data$treat_aid2 <- ifelse(data$country6=="another country", "control",
                          ifelse(data$country6=="Canada" | data$country6=="Great Britain" | data$country6=="Japan", "friend",
                                 ifelse(data$country6=="China" | data$country6=="Iran" | data$country6=="Russia", "adversary", NA)))

#Security
data$treat_security1 <- ifelse(data$country7=="another country", "control",
                               ifelse(data$country7=="Canada" | data$country7=="Great Britain" | data$country7=="Japan", "friend",
                                      ifelse(data$country7=="China" | data$country7=="Iran" | data$country7=="Russia", "adversary", NA)))
data$treat_security2 <- ifelse(data$country8=="another country", "control",
                               ifelse(data$country8=="Canada" | data$country8=="Great Britain" | data$country8=="Japan", "friend",
                                      ifelse(data$country8=="China" | data$country8=="Iran" | data$country8=="Russia", "adversary", NA)))


#Cooperation
data$treat_cooperation1 <- ifelse(data$country9=="another country", "control",
                                  ifelse(data$country9=="Canada" | data$country9=="Great Britain" | data$country9=="Japan", "friend",
                                         ifelse(data$country9=="China" | data$country9=="Iran" | data$country9=="Russia", "adversary", NA)))
data$treat_cooperation2 <- ifelse(data$country10=="another country", "control",
                                  ifelse(data$country10=="Canada" | data$country10=="Great Britain" | data$country10=="Japan", "friend",
                                         ifelse(data$country10=="China" | data$country10=="Iran" | data$country10=="Russia", "adversary", NA)))

#Economic
data$treat_economic1 <- ifelse(data$country11=="another country", "control",
                               ifelse(data$country11=="Canada" | data$country11=="Great Britain" | data$country11=="Japan", "friend",
                                      ifelse(data$country11=="China" | data$country11=="Iran" | data$country11=="Russia", "adversary", NA)))
data$treat_economic2 <- ifelse(data$country12=="another country", "control",
                               ifelse(data$country12=="Canada" | data$country12=="Great Britain" | data$country12=="Japan", "friend",
                                      ifelse(data$country12=="China" | data$country12=="Iran" | data$country12=="Russia", "adversary", NA)))





# Reshape the data into long format for primary analysis (moralization DV), save issue area subsets --------------------------------------------------------

data_long <- melt(data, id.vars=c("ResponseId", "treat_hr1", "treat_hr2", "treat_climate1", "treat_climate2", "treat_aid1", "treat_aid2",
                                  "treat_security1", "treat_security2", "treat_cooperation1", "treat_cooperation2", "treat_economic1", "treat_economic2",
                                  "man", "woman", "nhwhite", "gensilent", "genboom", "genx", "genmill", "genz", "pidreplean","piddemlean", 
                                  "pidtrueind", "university", "inc1", "inc2", "inc3", "inc4", "polint1", "mi1", "ci1", "isolat1", "rel_attendr",
                                  "rel_impr", "pid7", "regionf", "ideo", "country1", "country2", "country3", "country4", "country5", "country6",
                                  "country7", "country8", "country9", "country10", "country11", "country12"), 
                  measure.vars = c("hr1moral", "hr2moral", "climate1moral", "climate2moral", "aid1moral", "aid2moral",
                                   "security1moral", "security2moral", "cooperation1moral", "cooperation2moral", "economic1moral", "economic2moral"),
                  variable.name="issue", value.name="dv_moral")

data_long$treatment <- ifelse(data_long$issue=="hr1moral", data_long$treat_hr1,
                              ifelse(data_long$issue=="hr2moral", data_long$treat_hr1,
                                     ifelse(data_long$issue=="climate1moral", data_long$treat_climate1,
                                            ifelse(data_long$issue=="climate2moral", data_long$treat_climate2,
                                                   ifelse(data_long$issue=="aid1moral", data_long$treat_aid1,
                                                          ifelse(data_long$issue=="aid2moral", data_long$treat_aid2,
                                                                 ifelse(data_long$issue=="security1moral", data_long$treat_security1,
                                                                        ifelse(data_long$issue=="security2moral", data_long$treat_security2,
                                                                               ifelse(data_long$issue=="cooperation1moral", data_long$treat_cooperation1,
                                                                                      ifelse(data_long$issue=="cooperation2moral", data_long$treat_cooperation2,
                                                                                             ifelse(data_long$issue=="economic1moral", data_long$treat_economic1,
                                                                                                    ifelse(data_long$issue=="economic2moral", data_long$treat_economic2, NA))))))))))))
data_long$treatment <- factor(data_long$treatment, levels=c("control", "friend", "adversary"))

data_long$issuearea <- ifelse(data_long$issue=="hr1moral" | data_long$issue=="hr2moral", "hr",
                              ifelse(data_long$issue=="climate1moral" | data_long$issue=="climate2moral", "climate",
                                     ifelse(data_long$issue=="aid1moral" | data_long$issue=="aid2moral", "aid",
                                            ifelse(data_long$issue=="security1moral" | data_long$issue=="security2moral", "sec",
                                                   ifelse(data_long$issue=="cooperation1moral" | data_long$issue=="cooperation2moral", "coop",
                                                          ifelse(data_long$issue=="economic1moral" | data_long$issue=="economic2moral", "econ", NA))))))

data_long$issuearea <- factor(data_long$issuearea)
data_long$issuearea <- relevel(data_long$issuearea, ref="econ") #economics as baseline


data_long$hisalience <- ifelse(data_long$issue=="hr1moral" | data_long$issue=="climate1moral" | data_long$issue=="aid2moral" | data_long$issue=="security1moral" | data_long$issue=="cooperation1moral" | data_long$issue=="economic1moral", 1,
                               ifelse(data_long$issue=="hr2moral" | data_long$issue=="climate2moral" | data_long$issue=="aid1moral" | data_long$issue=="security2moral" | data_long$issue=="cooperation2moral" | data_long$issue=="economic2moral", 0, NA))


#For country analyses
data_long$countrytreat <- ifelse(data_long$issue=="hr1moral", data_long$country1,
                                 ifelse(data_long$issue=="hr2moral", data_long$country2,
                                        ifelse(data_long$issue=="climate1moral", data_long$country3,
                                               ifelse(data_long$issue=="climate2moral", data_long$country4,
                                                      ifelse(data_long$issue=="aid1moral", data_long$country5,
                                                             ifelse(data_long$issue=="aid2moral", data_long$country6,
                                                                    ifelse(data_long$issue=="security1moral", data_long$country7,
                                                                           ifelse(data_long$issue=="security2moral", data_long$country8,
                                                                                  ifelse(data_long$issue=="cooperation1moral", data_long$country9,
                                                                                         ifelse(data_long$issue=="cooperation2moral", data_long$country10,
                                                                                                ifelse(data_long$issue=="economic1moral", data_long$country11,
                                                                                                       ifelse(data_long$issue=="economic2moral", data_long$country12, NA))))))))))))

dat.hr <- subset(data_long, data_long$issuearea=="hr")
dat.climate <- subset(data_long, data_long$issuearea=="climate")
dat.aid <- subset(data_long, data_long$issuearea=="aid")
dat.security <- subset(data_long, data_long$issuearea=="sec")
dat.coop <- subset(data_long, data_long$issuearea=="coop")
dat.econ <- subset(data_long, data_long$issuearea=="econ")


#Remove NAs on DV for summary function
dat.hrsum <- subset(data_long, data_long$issuearea=="hr" & !is.na(data_long$dv_moral))
dat.climatesum <- subset(data_long, data_long$issuearea=="climate" & !is.na(data_long$dv_moral))
dat.aidsum <- subset(data_long, data_long$issuearea=="aid" & !is.na(data_long$dv_moral))
dat.secsum <- subset(data_long, data_long$issuearea=="sec" & !is.na(data_long$dv_moral))
dat.coopsum <- subset(data_long, data_long$issuearea=="coop" & !is.na(data_long$dv_moral))
dat.econsum <- subset(data_long, data_long$issuearea=="econ" & !is.na(data_long$dv_moral))






# # Reshape into long format for policy support DV, save issue area --------

data_long2 <- melt(data, id.vars=c("ResponseId", "treat_hr1", "treat_hr2", "treat_climate1", "treat_climate2", "treat_aid1", "treat_aid2",
                                   "treat_security1", "treat_security2", "treat_cooperation1", "treat_cooperation2", "treat_economic1", "treat_economic2",
                                   "man", "woman", "nhwhite", "gensilent", "genboom", "genx", "genmill", "genz", "pidreplean","piddemlean", 
                                   "pidtrueind", "university", "inc1", "inc2", "inc3", "inc4", "regionf", "polint1", "mi1", "ci1", "isolat1", "rel_attendr",
                                   "rel_impr"), 
                   measure.vars = c("hr1r", "hr2r", "climate1r", "climate2r", "aid1r", "aid2r",
                                    "security1r", "security2r", "cooperation1r", "cooperation2r", "economic1r", "economic2r"),
                   variable.name="issue", value.name="dv_support")

data_long2$treatment <- ifelse(data_long2$issue=="hr1r", data_long2$treat_hr1,
                               ifelse(data_long2$issue=="hr2r", data_long2$treat_hr2,
                                      ifelse(data_long2$issue=="climate1r", data_long2$treat_climate1,
                                             ifelse(data_long2$issue=="climate2r", data_long2$treat_climate2,
                                                    ifelse(data_long2$issue=="aid1r", data_long2$treat_aid1,
                                                           ifelse(data_long2$issue=="aid2r", data_long2$treat_aid2,
                                                                  ifelse(data_long2$issue=="security1r", data_long2$treat_security1,
                                                                         ifelse(data_long2$issue=="security2r", data_long2$treat_security2,
                                                                                ifelse(data_long2$issue=="cooperation1r", data_long2$treat_cooperation1,
                                                                                       ifelse(data_long2$issue=="cooperation2r", data_long2$treat_cooperation2,
                                                                                              ifelse(data_long2$issue=="economic1r", data_long2$treat_economic1,
                                                                                                     ifelse(data_long2$issue=="economic2r", data_long2$treat_economic2, NA))))))))))))
data_long2$treatment <- factor(data_long2$treatment, levels=c("control", "friend", "adversary"))

data_long2$issuearea <- ifelse(data_long2$issue=="hr1r" | data_long2$issue=="hr2r", "hr",
                               ifelse(data_long2$issue=="climate1r" | data_long2$issue=="climate2r", "climate",
                                      ifelse(data_long2$issue=="aid1r" | data_long2$issue=="aid2r", "aid",
                                             ifelse(data_long2$issue=="security1r" | data_long2$issue=="security2r", "sec",
                                                    ifelse(data_long2$issue=="cooperation1r" | data_long2$issue=="cooperation2r", "coop",
                                                           ifelse(data_long2$issue=="economic1r" | data_long2$issue=="economic2r", "econ", NA))))))

data_long2$issuearea <- factor(data_long2$issuearea)



dat.hr2 <- subset(data_long2, data_long2$issuearea=="hr")
dat.hr2$hisalience <- ifelse(dat.hr2$issue=="hr1r", 1,
                             ifelse(dat.hr2$issue=="hr2r", 0, NA))
                      
dat.climate2 <- subset(data_long2, data_long2$issuearea=="climate")
dat.climate2$hisalience <- ifelse(dat.climate2$issue=="climate1r", 1,
                             ifelse(dat.climate2$issue=="climate2r", 0, NA))

dat.aid2 <- subset(data_long2, data_long2$issuearea=="aid")
dat.aid2$hisalience <- ifelse(dat.aid2$issue=="aid2r", 1,
                                  ifelse(dat.aid2$issue=="aid1r", 0, NA))

dat.security2 <- subset(data_long2, data_long2$issuearea=="sec")
dat.security2$hisalience <- ifelse(dat.security2$issue=="security1r", 1,
                              ifelse(dat.security2$issue=="security2r", 0, NA))

dat.coop2 <- subset(data_long2, data_long2$issuearea=="coop")
dat.coop2$hisalience <- ifelse(dat.coop2$issue=="cooperation1r", 1,
                                   ifelse(dat.coop2$issue=="cooperation2r", 0, NA))

dat.econ2 <- subset(data_long2, data_long2$issuearea=="econ")
dat.econ2$hisalience <- ifelse(dat.econ2$issue=="economic1r", 1,
                                   ifelse(dat.econ2$issue=="economic2r", 0, NA))




# # Data subsets for demographic differences ------------------------------

#Gender
data.m <- subset(data_long, data_long$man==1)
data.f <- subset(data_long, data_long$woman==1) 

dat.hrm <- subset(data.m, data.m$issuearea=="hr") 
dat.hrf <- subset(data.f, data.f$issuearea=="hr") 

dat.aidm <- subset(data.m, data.m$issuearea=="aid") 
dat.aidf <- subset(data.f, data.f$issuearea=="aid") 

dat.climatem <- subset(data.m, data.m$issuearea=="climate") 
dat.climatef <- subset(data.f, data.f$issuearea=="climate") 

dat.secm <- subset(data.m, data.m$issuearea=="sec") 
dat.secf <- subset(data.f, data.f$issuearea=="sec") 

dat.coopm <- subset(data.m, data.m$issuearea=="coop") 
dat.coopf <- subset(data.f, data.f$issuearea=="coop") 

dat.econm <- subset(data.m, data.m$issuearea=="econ") 
dat.econf <- subset(data.f, data.f$issuearea=="econ") 

#Generation
data.old <- subset(data_long, data_long$gensilent==1 | data_long$genboom==1)
data.young <- subset(data_long, data_long$genmill==1 | data_long$genz==1) 

dat.hrold <- subset(data.old, data.old$issuearea=="hr") 
dat.hryoung <- subset(data.young, data.young$issuearea=="hr") 

dat.aidold <- subset(data.old, data.old$issuearea=="aid") 
dat.aidyoung <- subset(data.young, data.young$issuearea=="aid") 

dat.climateold <- subset(data.old, data.old$issuearea=="climate") 
dat.climateyoung <- subset(data.young, data.young$issuearea=="climate") 

dat.secold <- subset(data.old, data.old$issuearea=="sec") 
dat.secyoung <- subset(data.young, data.young$issuearea=="sec") 

dat.coopold <- subset(data.old, data.old$issuearea=="coop") 
dat.coopyoung <- subset(data.young, data.young$issuearea=="coop") 

dat.econold <- subset(data.old, data.old$issuearea=="econ") 
dat.econyoung <- subset(data.young, data.young$issuearea=="econ") 


#Party
data.rep <- subset(data_long, data_long$pidreplean==1)
data.dem <- subset(data_long, data_long$piddemlean==1) 

dat.hrrep <- subset(data.rep, data.rep$issuearea=="hr") 
dat.hrdem <- subset(data.dem, data.dem$issuearea=="hr") 

dat.aidrep <- subset(data.rep, data.rep$issuearea=="aid") 
dat.aiddem <- subset(data.dem, data.dem$issuearea=="aid") 

dat.climaterep <- subset(data.rep, data.rep$issuearea=="climate") 
dat.climatedem <- subset(data.dem, data.dem$issuearea=="climate") 

dat.secrep <- subset(data.rep, data.rep$issuearea=="sec") 
dat.secdem <- subset(data.dem, data.dem$issuearea=="sec") 

dat.cooprep <- subset(data.rep, data.rep$issuearea=="coop") 
dat.coopdem <- subset(data.dem, data.dem$issuearea=="coop") 

dat.econrep <- subset(data.rep, data.rep$issuearea=="econ") 
dat.econdem <- subset(data.dem, data.dem$issuearea=="econ") 


###Militant internationalism
hawks <- quantile(data$mi1, c(0.33, 0.5, 0.66))

data.hawk <- subset(data_long, data_long$mi1>=hawks[3])
data.dove <- subset(data_long, data_long$mi1<=hawks[1]) 

dat.hrhawk <- subset(data.hawk, data.hawk$issuearea=="hr") 
dat.hrdove <- subset(data.dove, data.dove$issuearea=="hr") 

dat.aidhawk <- subset(data.hawk, data.hawk$issuearea=="aid") 
dat.aiddove <- subset(data.dove, data.dove$issuearea=="aid") 

dat.climatehawk <- subset(data.hawk, data.hawk$issuearea=="climate") 
dat.climatedove <- subset(data.dove, data.dove$issuearea=="climate") 

dat.sechawk <- subset(data.hawk, data.hawk$issuearea=="sec") 
dat.secdove <- subset(data.dove, data.dove$issuearea=="sec") 

dat.coophawk <- subset(data.hawk, data.hawk$issuearea=="coop") 
dat.coopdove <- subset(data.dove, data.dove$issuearea=="coop") 

dat.econhawk <- subset(data.hawk, data.hawk$issuearea=="econ") 
dat.econdove <- subset(data.dove, data.dove$issuearea=="econ") 


#Cooperative Internationalism
citer <- quantile(data$ci1, c(0.33, 0.5, 0.66), na.rm=TRUE) #identify terciles

data.cih <- subset(data_long, data_long$ci1>=citer[3])
data.cil <- subset(data_long, data_long$ci1<=citer[1]) 

dat.hrcih <- subset(data.cih, data.cih$issuearea=="hr") 
dat.hrcil <- subset(data.cil, data.cil$issuearea=="hr") 

dat.aidcih <- subset(data.cih, data.cih$issuearea=="aid") 
dat.aidcil <- subset(data.cil, data.cil$issuearea=="aid") 

dat.climatecih <- subset(data.cih, data.cih$issuearea=="climate") 
dat.climatecil <- subset(data.cil, data.cil$issuearea=="climate") 

dat.seccih <- subset(data.cih, data.cih$issuearea=="sec") 
dat.seccil <- subset(data.cil, data.cil$issuearea=="sec") 

dat.coopcih <- subset(data.cih, data.cih$issuearea=="coop") 
dat.coopcil <- subset(data.cil, data.cil$issuearea=="coop") 

dat.econcih <- subset(data.cih, data.cih$issuearea=="econ") 
dat.econcil <- subset(data.cil, data.cil$issuearea=="econ") 

#Isolationism
isoter <- quantile(data$isolat1, c(0.33, 0.5, 0.66), na.rm=TRUE)

data.isoh <- subset(data_long, data_long$isolat1>=isoter[3])
data.isol <- subset(data_long, data_long$isolat1<=isoter[1]) 

dat.hrisoh <- subset(data.isoh, data.isoh$issuearea=="hr") 
dat.hrisol <- subset(data.isol, data.isol$issuearea=="hr") 

dat.aidisoh <- subset(data.isoh, data.isoh$issuearea=="aid") 
dat.aidisol <- subset(data.isol, data.isol$issuearea=="aid") 

dat.climateisoh <- subset(data.isoh, data.isoh$issuearea=="climate") 
dat.climateisol <- subset(data.isol, data.isol$issuearea=="climate") 

dat.secisoh <- subset(data.isoh, data.isoh$issuearea=="sec") 
dat.secisol <- subset(data.isol, data.isol$issuearea=="sec") 

dat.coopisoh <- subset(data.isoh, data.isoh$issuearea=="coop") 
dat.coopisol <- subset(data.isol, data.isol$issuearea=="coop") 

dat.econisoh <- subset(data.isoh, data.isoh$issuearea=="econ") 
dat.econisol <- subset(data.isol, data.isol$issuearea=="econ") 

#Exploratory: Race
data.w <- subset(data_long, data_long$nhwhite==1)
data.nw <- subset(data_long, data_long$nhwhite==0) 

dat.hrnw <- subset(data.nw, data.nw$issuearea=="hr") 
dat.hrw <- subset(data.w, data.w$issuearea=="hr") 

dat.aidnw <- subset(data.nw, data.nw$issuearea=="aid") 
dat.aidw <- subset(data.w, data.w$issuearea=="aid") 

dat.climatenw <- subset(data.nw, data.nw$issuearea=="climate") 
dat.climatew <- subset(data.w, data.w$issuearea=="climate") 

dat.secnw <- subset(data.nw, data.nw$issuearea=="sec") 
dat.secw <- subset(data.w, data.w$issuearea=="sec") 

dat.coopnw <- subset(data.nw, data.nw$issuearea=="coop") 
dat.coopw <- subset(data.w, data.w$issuearea=="coop") 

dat.econnw <- subset(data.nw, data.nw$issuearea=="econ") 
dat.econw <- subset(data.w, data.w$issuearea=="econ") 


# #Save the data ----------------------------------------------------------

write.csv(data, "MP_study1_coded.csv") 





